package org.example.controller;

import cn.jbit.pojo.Order;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.User;
import org.example.service.OrderService;
import org.example.service.UserService;
import org.example.utils.Constants;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.Future;


/**
 * @author 张在成
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {



    @Autowired
    private OrderService orderService;

//    @Autowired
//    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/order/{orderId}")
    public Result getOrderInfoWithUser(@PathVariable("orderId") Integer orderId, String flag) throws Exception {
/*      StringBuffer sb=null;
        ServiceInstance serviceInstance=loadBalancerClient.choose("order-service");

        if (serviceInstance==null){
            return  null;
        }
        sb=new StringBuffer();
        sb.append("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/order/"+orderId);
        System.out.println(sb.toString());

        Result result = restTemplate.getForObject(sb.toString(), Result.class);
        ResponseEntity<Order> responseEntity=restTemplate.exchange(sb.toString(), HttpMethod.GET, null, new ParameterizedTypeReference<Order>() {});*/

        log.info("根据订单id查询订单信息："+orderId);
        Future<Order> order = orderService.selectOrderByOrderId(orderId);

        if(order==null){
            return  new Result(Constants.FAIL_STATUS_CODE,Constants.FAIL_MESSAGE,order);
        }

        //return  responseEntity.getBody();
        return  new Result(Constants.SUCESS_STATUS_CODE,Constants.SUCESS_MESSAGE,order);

    }

    //rest方式获取用户全部订单信息
    @RequestMapping(value = "/allOrder")
    public Result getUserInfo() throws Exception {
        List<Order>  orderList=orderService.getOrderList();
        if(orderList==null){
            return  new Result(Constants.FAIL_STATUS_CODE,Constants.FAIL_MESSAGE,orderList);
        }
        return  new Result(Constants.SUCESS_STATUS_CODE,Constants.SUCESS_MESSAGE,orderList);
    }


    //rest方式根据ids获取全部订单信息
    @RequestMapping(value = "/orderIds")
    public Result getUserInfoByOrderIds(@RequestBody List<Integer> list) throws Exception {
        List<Order>  orderList=orderService.selOrderListByIds(list);
        if(orderList==null){
            return  new Result(Constants.FAIL_STATUS_CODE,Constants.FAIL_MESSAGE,orderList);
        }
        return  new Result(Constants.SUCESS_STATUS_CODE,Constants.SUCESS_MESSAGE,orderList);
    }





}
